import {hashHistory} from 'dva/router';
import {busQuery,phoneQuery,weChartQuery,growQuery,purchaseQuery,newMatchQuery,matchGrowQuery} from '../services/BusinessData';

import {
  parse
} from 'qs';

import error from '../utils/tips';
// import qs from 'qs';



export default{
	namespace:'BusinessData',

	state:{
		busData:{},
		phoneData:{},
		weChartData:{},
		growData:{},
		purchaseData:{},
		newMatchData:{},
		matchGrowData:{},

		phoneLoading:false,
		weChartLoading:false,
		growDataLoading:false,
		purchaseLoading:false,
		matchGrowLoading:false,
	},

	subscriptions:{
	},

	effects:{
		*busQuery({payload},{call,put}){
			const {data} = yield call(busQuery, parse(payload));
			if(data){
				yield put({
					type:'busQuerySuccess',
					payload:{
						busData:data.data,
					}
				})
			}
		},
		*phoneQuery({payload},{call,put}){
			yield put({type:'phoneLoading'});
			const {data} = yield call(phoneQuery, parse(payload));
			if(data){
				yield put({
					type:'phoneQuerySuccess',
					payload:{
						phoneData:data.data,
					}
				})
			}
		},
		*weChartQuery({payload},{call,put}){
			yield put({type:'weChartLoading'});
			const {data} = yield call(weChartQuery, parse(payload));
			if(data){
				yield put({
					type:'weChartQuerySuccess',
					payload:{
						weChartData:data.data,
					}
				})
			}
		},
		*growQuery({payload},{call,put}){
			yield put({type:'growDataLoading'});
			const {data} = yield call(growQuery, parse(payload));
			if(data){
				yield put({
					type:'growQuerySuccess',
					payload:{
						growData:data.data,
					}
				})
			}else {
		        error("获取活动主题偏好失败");
		      }
		},
		*purchaseQuery({payload},{call,put}){
			yield put({type:'purchaseLoading'});
			const {data} = yield call(purchaseQuery, parse(payload));
			if(data){
				yield put({
					type:'purchaseQuerySuccess',
					payload:{
						purchaseData:data.data,
					}
				})
			}
		},
		*newMatchQuery({payload},{call,put}){
			const {data} = yield call(newMatchQuery, parse(payload));
			if(data){
				yield put({
					type:'newMatchQuerySuccess',
					payload:{
						newMatchData:data.data,
					}
				})
			}
		},
		*matchGrowQuery({payload},{call,put}){
			yield put({type:'matchGrowLoading'});
			const {data} = yield call(matchGrowQuery, parse(payload));
			if(data){
				yield put({
					type:'matchGrowQuerySuccess',
					payload:{
						matchGrowData:data.data,
					}
				})
			}
		},
	},

	reducers:{
		phoneLoading(state){
	      return {...state,phoneLoading:true};
	    },
	    weChartLoading(state){
	      return {...state,weChartLoading:true};
	    },
	    growDataLoading(state){
	      return {...state,growDataLoading:true};
	    },
	    purchaseLoading(state){
	      return {...state,purchaseLoading:true};
	    },
	    matchGrowLoading(state){
	      return {...state,matchGrowLoading:true};
	    },

		busQuerySuccess(state,action){
			return {...state,...action.payload}
		},
		phoneQuerySuccess(state,action){
			return {...state,...action.payload,phoneLoading:false}
		},
		weChartQuerySuccess(state,action){
			return {...state,...action.payload,weChartLoading:false}
		},
		growQuerySuccess(state,action){
			return {...state,...action.payload,growDataLoading:false}
		},
		purchaseQuerySuccess(state,action){
			return {...state,...action.payload,purchaseLoading:false}
		},
		newMatchQuerySuccess(state,action){
			return {...state,...action.payload}
		},
		matchGrowQuerySuccess(state,action){
			return {...state,...action.payload,matchGrowLoading:false}
		},
	}
}